Page({
    data: {
        active: 0,
        btnLoading: false, //订单确认退款按钮loading
        showCancel: false,
        state: 0,
        orderList: [],
        orderLIstLoading: false,
        orderId: 0, //订单id
        cancelText: '', //取消原因
        paramsData() {
            return {
                state: this.data.state,
            };
        },
    },

    onLoad(options) {
        this.setData({
            active: options.mark,
            state: options.mark,
        });
    },
    onShow() {
        // 刷新订单列表
        this.paging.reRequest();
    },
    onTabsChange(e) {
        const val = e.detail.value;
        console.log(val);
        if (val == 6) {
            this.setData({
                state: -1,
                active: val,
            });
        } else {
            this.setData({
                active: val,
                state: val,
            });
        }

        this.paging.reRequest();
    },

    // 获取订单列表
    getOrderList() {
        this.setData({
            orderLIstLoading: true,
        });
        this.request({
            url: 'user/my/order/index',
            data: {
                state: this.data.state,
            },
        })
            .then(res => {
                console.log(res.data.data);
                this.setData({
                    orderLIstLoading: false,
                    orderList: res.data.data,
                });
            })
            .catch(err => {
                this.setData({
                    orderLIstLoading: true,
                });
            });
    },

    // 打击去订单详情页面
    toOrderDetail(e) {
        const active = e.currentTarget.dataset.active;
        const state = e.currentTarget.dataset.state;
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/orderDetail/orderDetail?active=' + active + '&state=' + state + '&id=' + id + '&pay_wx_id=' + e.currentTarget.dataset.pay_id,
        });
    },

    // 点击取消订单按钮
    onCancelOrder(e) {
        this.setData({
            orderId: e.currentTarget.dataset.id,
            showCancel: true,
        });
    },

    // 确认取消按钮
    onSubmitCancael() {
        if (!this.data.cancelText)
            return wx.showToast({
                title: '请填写取消原因',
                icon: 'none',
            });

        this.setData({
            btnLoading: true,
        });
        this.request({
            url: 'user/my/order/cancel',
            method: 'POST',
            data: {
                id: this.data.orderId,
                cancel_cause: this.data.cancelText,
            },
        })
            .then(res => {
                // console.log(res);
                // 提示信息
                wx.showToast({
                    title: res.msg,
                });
                // 关闭loading、弹出层
                this.setData({
                    btnLoading: false,
                    showCancel: false,
                });
                // 刷新列表
                this.getOrderList();
            })
            .catch(err => {
                // 关闭loading、弹出层
                this.setData({
                    btnLoading: false,
                    showCancel: false,
                });
            });
    },

    onInputCancel(e) {
        // console.log();
        this.setData({
            cancelText: e.detail.value,
        });
    },

    onClosePopup() {
        this.setData({
            showCancel: false,
        });
    },

    // 确认收货按钮
    onSubmitTake(e) {
       
        if (wx.openBusinessView) {
           
            const extraData = {
                merchant_id: e.currentTarget.dataset.id, // 用户交易商户号
                merchant_trade_no: e.currentTarget.dataset.id, // 商户系统的订单号
            };

            wx.openBusinessView({
                businessType: 'weappOrderConfirm',
                extraData,
                success(res) {
                   console.log('成功');
                },
                fail(err) {
                    console.error('打开确认收货组件失败：', err);
                },
            });
        } else {
           
            wx.showModal({
                title: '提示',
                content: '您的微信版本过低，请升级到最新版以使用确认收货功能',
                showCancel: false,
                confirmText: '去升级',
            });
        }
    return
        let id = e.currentTarget.dataset.id;
        wx.showLoading({
            title: '加载中...',
        });
        this.request({
            url: 'user/my/order/confirmReceive',
            method: 'POST',
            data: {
                id,
            },
        })
            .then(res => {
                // 提示
                wx.showToast({
                    title: res.msg,
                });

                // 刷新列表
                this.paging.reRequest();
            })
            .catch(err => {
                wx.hideLoading();
            });
    },

    // 立即支付按钮
    lijiPay(e) {
        wx.showLoading({
            title: '支付中...',
        });
        this.request({
            url: 'user/my/order/continuePay',
            method: 'POST',
            data: {
                id: e.currentTarget.dataset.lijiid,
            },
        })
            .then(res => {
                if (res.data.pay_data == null)
                    return wx.showToast({
                        title: '该订单仅支持线下支付',
                        icon: 'none',
                    });

                const { timeStamp, paySign, signType, package: packageId, nonceStr } = res.data.pay_data;
                wx.requestPayment({
                    timeStamp: String(timeStamp),
                    nonceStr,
                    package: packageId,
                    signType,
                    paySign,
                    success: async res => {
                        wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                        });
                        // 刷新订单列表
                        this.paging.reRequest();
                    },
                    fail: async err => {
                        wx.showToast({
                            title: '支付失败',
                            icon: 'none',
                        });
                    },
                });

                this.setData({
                    payLoading: false,
                });
            })
            .catch(err => {
                wx.hideLoading();
            })
            .finally(fll => {});
    },
    async onPullDownRefresh() {
        this.getOrderList();
        await this.sleep(1200);
        wx.stopPullDownRefresh();
    },
});
